const router = require('express').Router()
const bcrypt = require('bcrypt')
const Admin = require('./../sql/models/Admin')
const sql = require('./../sql')
const utils = require('./../utils/index')
const { insert } = require('./../sql')

/**
 * @api {Post} /api/admin/login 管理系统登录
 * @apiName PostCategory
 * @apiGroup Admin
 * 
 * @apiParam { string } adminname
 *  管理员账号
 * @apiParam { string } password 密码
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200" || '10003',
 *       "message": "登录成功" || 输入信息有误,
 *     }
 * @apiSampleRequest /api/admin/login
 */
router.post('/login', (req, res, next) => {
  let { adminname, password } = req.body

  sql.find(Admin, { adminname }, { _id: 0, __v: 0 }).then(data => {
    if (data.length !== 0) {
      bcrypt.compare(password, data[0].password).then(result => {
        if (result) { // 密码验证通过 --- 通过
          const token=utils.createToken({adminname},'就nm离谱',{
            expiresIn:10*60
          })
          res.status(200).send({
            code: '200',
            message: '登录成功',
            data: {
              adminname,
              token,
              role: data[0].role
            }
          })
        } else {
          res.status(200).send({
            code: '10004',
            message: '密码错误'
          })
        }
      })
    } else {
      res.status(200).send({
        code: '10003',
        message: '没有该管理员账号'
      })
    }
  })
})

/**
 * @api {Post} /api/admin/add 添加管理员
 * @apiName PostAddAdmin
 * @apiGroup Admin
 * 
 * @apiParam { string } adminname
 * @apiParam { string } password
 * @apiParam { string } role
 * @apiParam { string } checkedKeys
 * 
 * @apiHeader { string } token
 * 
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200" || '10004',
 *       "message": "添加管理员成功" || 该管理员已存在,
 *     }
 * @apiSampleRequest /api/admin/add
 */
router.post('/add', (req, res, next) => {
  let insertData = req.body

  let password = insertData.password
  insertData.adminid = 'admin_' + utils.getUuid()

  utils.encryption(password).then((result) => { // 密码加密
    insertData.password = result
    // console.log(insertData)
    // 判断有没有该管理员账号
    sql.find(Admin, { adminname: insertData.adminname}, {_id: 0, __v: 0}).then(data => {
      if (data.length === 0) { // 没有当前想要添加的管理员
        sql.insert(Admin, insertData).then(() => {
          res.status(200).send({
            code: '200',
            message: '添加管理员成功'
          })
        })
      } else { // 已有该管理员
        res.status(200).send({
          code: '10004',
          message: '该管理员已存在'
        })
      }
    })
  })
  
})
router.get('/detail', (req, res, next) => {
  const { adminname } = req.query
  sql.find(Admin, { adminname }, { _id: 0, __v: 0, password: 0}).then(data => {
    res.status(200).send({
      code: '200',
      message: '获取管理员信息',
      data
    })
  })
})
router.get('/list', (req, res, next) => {
  sql.find(Admin, {}, { _id: 0, __v: 0, password: 0}).then(data => {
    res.status(200).send({
      code: '200',
      message: '获取管理员列表信息',
      data
    })
  })
})
router.post('/update', (req, res, next) => {
  let updateData = req.body
  sql.update(Admin, { adminname: updateData.adminname }, { $set: updateData}).then(() => {
    res.status(200).send({
      code: '200',
      message: '修改管理员权限成功'
    })
  })
})
module.exports = router